<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产主体" prop="productionEntity">
        <el-input
          v-model="queryParams.productionEntity"
          placeholder="请输入生产主体"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input
          v-model="queryParams.legalPersonName"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县" prop="district">
        <el-input
          v-model="queryParams.district"
          placeholder="请输入区/县"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="镇/乡" prop="town">
        <el-input
          v-model="queryParams.town"
          placeholder="请输入镇/乡"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="detailedAddress">
        <el-input
          v-model="queryParams.detailedAddress"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产地址" prop="productionAddress">
        <el-input
          v-model="queryParams.productionAddress"
          placeholder="请输入生产地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="食用农产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入食用农产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入重量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合格方式" prop="certificationMethod">
        <el-input
          v-model="queryParams.certificationMethod"
          placeholder="请输入合格方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验机构" prop="inspectionAgency">
        <el-input
          v-model="queryParams.inspectionAgency"
          placeholder="请输入检验机构"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测报告" prop="testReport">
        <el-input
          v-model="queryParams.testReport"
          placeholder="请输入检测报告"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开具日期" prop="issueDate">
        <el-date-picker clearable
          v-model="queryParams.issueDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开具日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合格证标号" prop="certificateNumber">
        <el-input
          v-model="queryParams.certificateNumber"
          placeholder="请输入合格证标号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印次数" prop="printCount">
        <el-input
          v-model="queryParams.printCount"
          placeholder="请输入打印次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫描次数" prop="scanCount">
        <el-input
          v-model="queryParams.scanCount"
          placeholder="请输入扫描次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入操作人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['nongjt:certification:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:certification:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:certification:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['nongjt:certification:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号" align="center" prop="account" />
      <el-table-column label="生产主体" align="center" prop="productionEntity" />
      <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="district" />
      <el-table-column label="镇/乡" align="center" prop="town" />
      <el-table-column label="详细地址" align="center" prop="detailedAddress" />
      <el-table-column label="生产地址" align="center" prop="productionAddress" />
      <el-table-column label="食用农产品名称" align="center" prop="productName" />
      <el-table-column label="重量" align="center" prop="quantity" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="合格方式" align="center" prop="certificationMethod" />
      <el-table-column label="检验机构" align="center" prop="inspectionAgency" />
      <el-table-column label="检测报告" align="center" prop="testReport" />
      <el-table-column label="开具日期" align="center" prop="issueDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合格证标号" align="center" prop="certificateNumber" />
      <el-table-column label="打印次数" align="center" prop="printCount" />
      <el-table-column label="扫描次数" align="center" prop="scanCount" />
      <el-table-column label="操作人" align="center" prop="operator" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nongjt:certification:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nongjt:certification:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合格证打印列对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="certificationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="生产主体" prop="productionEntity">
          <el-input v-model="form.productionEntity" placeholder="请输入生产主体" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区/县" prop="district">
          <el-input v-model="form.district" placeholder="请输入区/县" />
        </el-form-item>
        <el-form-item label="镇/乡" prop="town">
          <el-input v-model="form.town" placeholder="请输入镇/乡" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailedAddress">
          <el-input v-model="form.detailedAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="生产地址" prop="productionAddress">
          <el-input v-model="form.productionAddress" placeholder="请输入生产地址" />
        </el-form-item>
        <el-form-item label="食用农产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入食用农产品名称" />
        </el-form-item>
        <el-form-item label="重量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="合格方式" prop="certificationMethod">
          <el-input v-model="form.certificationMethod" placeholder="请输入合格方式" />
        </el-form-item>
        <el-form-item label="检验机构" prop="inspectionAgency">
          <el-input v-model="form.inspectionAgency" placeholder="请输入检验机构" />
        </el-form-item>
        <el-form-item label="检测报告" prop="testReport">
          <el-input v-model="form.testReport" placeholder="请输入检测报告" />
        </el-form-item>
        <el-form-item label="开具日期" prop="issueDate">
          <el-date-picker clearable
            v-model="form.issueDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开具日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合格证标号" prop="certificateNumber">
          <el-input v-model="form.certificateNumber" placeholder="请输入合格证标号" />
        </el-form-item>
        <el-form-item label="打印次数" prop="printCount">
          <el-input v-model="form.printCount" placeholder="请输入打印次数" />
        </el-form-item>
        <el-form-item label="扫描次数" prop="scanCount">
          <el-input v-model="form.scanCount" placeholder="请输入扫描次数" />
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Certification">
import { listCertification, getCertification, delCertification, addCertification, updateCertification } from "@/api/nongjt/certification"

const { proxy } = getCurrentInstance()

const certificationList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    account: null,
    productionEntity: null,
    legalPersonName: null,
    province: null,
    city: null,
    district: null,
    town: null,
    detailedAddress: null,
    productionAddress: null,
    productName: null,
    quantity: null,
    unit: null,
    certificationMethod: null,
    inspectionAgency: null,
    testReport: null,
    issueDate: null,
    certificateNumber: null,
    printCount: null,
    scanCount: null,
    operator: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    account: [
      { required: true, message: "账号不能为空", trigger: "blur" }
    ],
    productionEntity: [
      { required: true, message: "生产主体不能为空", trigger: "blur" }
    ],
    legalPersonName: [
      { required: true, message: "法人姓名不能为空", trigger: "blur" }
    ],
    province: [
      { required: true, message: "省不能为空", trigger: "blur" }
    ],
    city: [
      { required: true, message: "市不能为空", trigger: "blur" }
    ],
    district: [
      { required: true, message: "区/县不能为空", trigger: "blur" }
    ],
    detailedAddress: [
      { required: true, message: "详细地址不能为空", trigger: "blur" }
    ],
    productionAddress: [
      { required: true, message: "生产地址不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "食用农产品名称不能为空", trigger: "blur" }
    ],
    quantity: [
      { required: true, message: "重量不能为空", trigger: "blur" }
    ],
    unit: [
      { required: true, message: "单位不能为空", trigger: "blur" }
    ],
    certificationMethod: [
      { required: true, message: "合格方式不能为空", trigger: "blur" }
    ],
    issueDate: [
      { required: true, message: "开具日期不能为空", trigger: "blur" }
    ],
    certificateNumber: [
      { required: true, message: "合格证标号不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询合格证打印列列表 */
function getList() {
  loading.value = true
  listCertification(queryParams.value).then(response => {
    certificationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    account: null,
    productionEntity: null,
    legalPersonName: null,
    province: null,
    city: null,
    district: null,
    town: null,
    detailedAddress: null,
    productionAddress: null,
    productName: null,
    quantity: null,
    unit: null,
    certificationMethod: null,
    inspectionAgency: null,
    testReport: null,
    issueDate: null,
    certificateNumber: null,
    printCount: null,
    scanCount: null,
    operator: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("certificationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加合格证打印列"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCertification(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改合格证打印列"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["certificationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCertification(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCertification(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除合格证打印列编号为"' + _ids + '"的数据项？').then(function() {
    return delCertification(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nongjt/certification/export', {
    ...queryParams.value
  }, `certification_${new Date().getTime()}.xlsx`)
}

getList()
</script>
